package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketOrderGoods;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.param.CommonData;

import com.cskaoyan.bean.wx.WXOrderSubmitBo;

import com.cskaoyan.bean.wxorder.WXOrderCommentPO;
import com.cskaoyan.bean.wxorder.WXOrderDetailVO;

import com.cskaoyan.bean.wxorder.WXOrderListVO;
import com.cskaoyan.service.wx.WXOrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author : Caspar
 * @since : 2022.06.28 17:42
 */
@RestController
@RequestMapping("wx/order")
public class WXOrderController {
    @Autowired
    WXOrderService wxOrderService;

    /**
     * 显示所有订单的详情  wx/order/list?showType=0&page=1&limit=10
     *
     * @param showType
     * @param basePageInfo
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/28 21:44
     */
    @RequestMapping("list")
    public BaseRespVo list(Integer showType, BasePageInfo basePageInfo) {

        // 从shiro中拿到用户的id信息
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if (StringUtils.isEmpty(principals)) {
            return BaseRespVo.error(501, "请先登录！");
        }
        MarketUser marketUser = (MarketUser) principals.getPrimaryPrincipal();

        Integer userId = marketUser.getId();

        CommonData<WXOrderListVO> data = wxOrderService.list(showType, basePageInfo, userId);
        return BaseRespVo.ok(data);
    }

    /**
     * 根据订单id，显示订单详情
     *
     * @param orderId
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/28 21:47
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) {
        WXOrderDetailVO wxOrderDetailVO = wxOrderService.detail(orderId);
        return BaseRespVo.ok(wxOrderDetailVO);
    }


    /**
     * 用户提交订单，但还未付款，此时可以取现订单。（但由于前端设计，此接口无对应按钮）
     *
     * @param map
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/29 20:45
     */
    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        wxOrderService.cancel(orderId);
        return BaseRespVo.ok();
    }

    /**
     * 根据订单id，用户申请退款，map里封装的是{"orderId":72}
     *
     * @param map
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/28 21:00
     */
    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");

        wxOrderService.refund(orderId);
        return BaseRespVo.ok();
    }

    /**
     * 根据订单id，用户删除订单，map里封装的是{"orderId":72}
     *
     * @param map
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/28 20:46
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");

        wxOrderService.delete(orderId);
        return BaseRespVo.ok();
    }

    /**
     * 根据订单id，确认订单收货，map里封装的是{"orderId":72}
     *
     * @param map
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/28 20:10
     */
    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");

        wxOrderService.confirm(orderId);
        return BaseRespVo.ok();
    }

    /**
     * 点击商品评论按钮，goods接口，从 market_order_goods 表中返回订单的相信信息
     *
     * @param orderId
     * @param goodsId
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/28 23:37
     */
    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId, Integer goodsId) {

        MarketOrderGoods marketOrderGoods = wxOrderService.goods(orderId, goodsId);
        return BaseRespVo.ok(marketOrderGoods);
    }

    /**
     * 评论发表按钮，触发comment接口，对商品进行评论
     *
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author: Caspar CXC
     * @since 2022/06/29 17:13
     */
    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody WXOrderCommentPO wxOrderCommentPO) {

        // 从shiro中拿到用户的id信息(评论表中插入数据需要userId）
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if (StringUtils.isEmpty(principals)) {
            return BaseRespVo.error(501, "请先登录！");
        }
        MarketUser marketUser = (MarketUser) principals.getPrimaryPrincipal();

        Integer userId = marketUser.getId();

        wxOrderService.comment(wxOrderCommentPO, userId);
        return BaseRespVo.ok();
    }

    /**
     * 此方法用于提交订单
     * 请求体 json
     * addressId: 18
     * cartId: 0
     * couponId: 67
     * grouponLinkId: 0
     * grouponRulesId: 2
     * message: ""
     * userCouponId: 147
     *
     * @param wxOrderSubmitBo
     * @return com.cskaoyan.bean.BaseRespVo
     * @author Can
     * @since 2022/06/29 19:35
     */
    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody WXOrderSubmitBo wxOrderSubmitBo) {

        BaseRespVo baseRespVo = wxOrderService.submitOrder(wxOrderSubmitBo);
        return baseRespVo;
    }

    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map map) {
        Object o = map.get("orderId");

        // 购物车付款传入的是Integer类型
        // 待付款传入的是String类型
        Integer orderId = 0;
        if (o instanceof String) {
            String id = (String) o;
            orderId = Integer.parseInt(id);
        } else if (o instanceof Integer) {
            orderId = (Integer) o;
        }

        BaseRespVo baseRespVo = wxOrderService.prepay(orderId);
        return baseRespVo;
    }
}
